<?php
class DataAction extends ZMAction {
    public function getDef(){
        $model = M('Yc');
        $res = $model->field('id,sname,dname')->order('id')->select();
        foreach($res as &$row){
            $row['text'] = '[' . $row['sname'] .']' .$row[dname];
        }
        $this->ajaxRetArray($res);
    }
    
    public function getdata(){
        $sids = str_replace("\\\"","\"",$_REQUEST['ids']);
        $sids = str_replace("\\\\","\\",$sids);
        $sids = "[$sids]";
        $ids = json_decode($sids,true);
        $model = M('Yc');
        $result = array();
        foreach($ids as $id){
            $where['id'] = $id['val'];
            $res = $model->where($where)->find();
            if($res){
                $data['id'] = $id['id'];
                $data['val'] = $res['val'];
                $result[] = $data;
            }
        }
        $this->ajaxRetArray($result);
    }
    
    public function getall(){
        $model = M('Yc');
        $res = $model->field('id,val')->select();
        $this->ajaxRetArray($res);
    }
    
    public function getUnitName($dname){
        if(strpos($dname,'电压')!== false){
            return 'V';
        }else if(strpos($dname,'电流')!==false){
            return 'A';
        }else if(strpos($dname,'温度')!==false){
            return '℃';
        }else if(strpos($dname,'湿度')!==false){
            return '%';
        }else if(strpos($dname,'功率')!==false){
            return 'VA';
        }else{
            return '';
        }
    }
    
    public function total(){
        $model = M('Yc');
        $res = $model->order('id')->select();
        $s = '';
        $last =null;
        if($res){
            foreach($res as $row){
                $dname = $row['dname'];
                if($last!=$row['sname']){
                    $last = $row['sname'];
                    if($s!=''){
                        $s .= '</tbody></table></div>';
                    }
                    $s .= '<div class="roomitem"><h1>'.$row['sname'].'</h1><table cellspacing="0" class="room-table"><thead><tr style="background:#eeeeee;"><th>项目名称</th><th style="width: 80px;">数值</th></tr></thead><tbody>';
                }
                $s.= '<tr><td>'.$row['dname'].'</td><td><span id="d'.$row['id'].'">'. $row['val'] .'</span> '. $this->getUnitName($dname) .'</td></tr>';
            }
        }
        if($s!=''){
            $s .= '</tbody></table></div>';
        }
        echo $s;
    }
    
    public function history(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $startdate = $_REQUEST['startdate'];
        if(empty($startdate)){
            $startdate = date('Y-').'01-01';
        }
        $enddate = $_REQUEST['enddate'];
        if(empty($enddate)){
            $enddate = date('Y-m-d');
        }
        $tid = $_REQUEST['tid'];
        if(!is_numeric($tid)){
            $this->ajaxRetArray(array()) ;
            return;
        }
        $Model = M("YcH$tid");
        $ret = array();
        
        $sec = strtotime($enddate)-strtotime($startdate);
        $count = $sec / (60*60*24) + 1;
        
        $firstdate = date('Y-m-d',strtotime('+'.$start .' day',strtotime($startdate)));
        $lastdate = date('Y-m-d',strtotime('+'.($start+$limit) .' day',strtotime($startdate)));
        
        for(;$firstdate<=$enddate && $firstdate<$lastdate;){
            $nextday = date('Y-m-d',strtotime('+1 day',strtotime($firstdate)));
            $where['_string'] = "ptime>='$firstdate' and ptime<'$nextday'";
            $res = $Model->where($where)->select();
            $data = array();
            $data['pdate'] = $firstdate;
            foreach($res as $row){
                $dt = strtotime($row['ptime']);
                $min = date('i',$dt);
                $hour = date('G',$dt);
                if($min == '00'){
                    $data["t$hour"] = $row['val'];
                }
            }
            $ret[] = $data;
            $firstdate = $nextday;
        }
        
        
        $this->ajaxRetArray($ret,$count);    
        
    }
}
